﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace AsteroidWar
{
    class VerticalBackGround
    {
        List<Sprite> BackgroundSprites;
        Sprite TopMostSprite;
        Sprite BottomMostSprite;
        float scale = 2f;
        float padding = 110f;
        //The viewing area for drawing the scrolling background images
        Viewport viewport;

        public VerticalBackGround(Viewport viewport, List<Sprite> BackgroundSprites)
        {
            this.BackgroundSprites = BackgroundSprites;
            this.TopMostSprite = null;
            this.BottomMostSprite = null;
            this.viewport = viewport;
        }

        public void adjustBackgroundLocation()
        {
            TopMostSprite = null;
            BottomMostSprite = null;

            //Cycle through all of the Background sprites to adjust their position.
            foreach (Sprite aBackgroundSprite in BackgroundSprites)
            {
                //If the Background sprite is the first in line, then TopMostSprite will be null.
                if (TopMostSprite == null)
                {
                    //Position the first Background sprite in line at the (0,0) position
                    aBackgroundSprite.Location = new Vector2(viewport.X + padding, viewport.Y);
                    BottomMostSprite = aBackgroundSprite;
                }
                else
                {
                    //Position the sprite after the top sprite in line
                    aBackgroundSprite.Location = new Vector2(viewport.X + padding, TopMostSprite.Location.Y - TopMostSprite.Source.Height * scale);
                }

                //Set the sprite as the top one in line
                TopMostSprite = aBackgroundSprite;
            }           
        }

        public void Update(GameTime theGameTime)
        {
            //Check to see if any of the Background sprites have moved off the screen          
            foreach (Sprite aBackgroundSprite in BackgroundSprites)
            {
                //If they have, then move them to the top of the chain of scrolling backgrounds
                if (aBackgroundSprite.Location.Y > viewport.Y + aBackgroundSprite.Source.Height * scale)
                {
                    aBackgroundSprite.Location = new Vector2(viewport.X + padding, TopMostSprite.Location.Y - TopMostSprite.Source.Height * scale);
                    TopMostSprite = aBackgroundSprite;
                }

                aBackgroundSprite.Update(theGameTime);
            }
        }

        public void Draw(SpriteBatch theSpriteBatch)
        {
            foreach (Sprite aBackgroundSprite in BackgroundSprites)
            {
                aBackgroundSprite.Draw(theSpriteBatch, scale);
            }
        }
    }
}
